package pers.fish.sso.client.filter;

import org.apache.commons.lang3.StringUtils;
import pers.fish.sso.common.util.HTTPUtil;
import pers.fish.sso.common.util.TicketUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 单点登出过滤器
 *
 * @author fish
 * @date 2020/1/30 20:44
 */
public class LogoutFilter implements Filter {

    private static final String LOGOUT_PARAMETER = "_LOGOUT_PARAMETER";

    private static final String LOGOUT_REQUEST_METHOD = "POST";

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

        if(!checkLogoutRequest(req)){
            chain.doFilter(req, resp);
            return;
        }

        //TODO 单点登出逻辑
        chain.doFilter(req, resp);
    }

    private boolean checkLogoutRequest(ServletRequest req) {
        HttpServletRequest request = (HttpServletRequest) req;
        String method = request.getMethod();
        if(!method.equalsIgnoreCase(LOGOUT_REQUEST_METHOD)){
            return false;
        }
        String parameter = request.getParameter(LOGOUT_PARAMETER);
        return !StringUtils.isBlank(parameter);
    }

}
